﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using OT.Common;
using OT.Dev;
using DevExpress.Xpo;
using OT.Module.Model;
using OT.BaseLibrary;

public partial class HugeData_HugeDataChange : DevPage
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Random rand = new Random();

        int number = Convert.ToInt32(txtNumber.Text);

        /***
         *  使用UnitOfWork是标准的批量操作方式
         * 
         */
        using (UnitOfWork uow = new UnitOfWork(XpoDefault.DataLayer))
        {
            int num = 0;
            for (int i = 0; i < number; i++)
            {
                Product obj = new Product(uow);
                obj.ID = CommonService.ReturnMax();
                obj.Name = "产品" + rand.Next(200, 20000).ToString();
                obj.DivValue = Convert.ToInt32(i / 1000) + 1;
                obj.Price = rand.Next(1, 5000);
                obj.StoreTime = DateTime.Now.AddDays(-1 * rand.Next(1, 300));

                // 每一万条记录批量处理一次
                num++;

                if (num % 10000 == 0)
                {
                    uow.CommitChanges();
                    num = 0;
                }
            }
            uow.CommitChanges();
        }

    }
}
